﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using iLangl.Model;
using iLangl.LanguageWire.Managers;

namespace iLangl.LanguageWire.DAL.LanguageWireDAL.MockupStorage
{
	public class MockupProjectDAL : MockupBaseDAL, IProjectDAL
	{
		#region IProjectDAL Members

		public void CreateProject(ProjectLW project, IDbTransaction transaction)
		{
			dtoMockupStorage.ProjectRow projectRow = Storage.Project.FindById(project.Instance.Id);
			bool isNew = (projectRow == null);
			if (isNew)
			{
				projectRow = Storage.Project.NewProjectRow();
			}

			projectRow.Deadline = project.Instance.Deadline;
			projectRow.Description = project.Instance.Description;
			projectRow.Id = project.Instance.Id;
			projectRow.Name = project.Instance.Name;


			if (project.Instance.Owner != null)
				projectRow.OwnerId = project.Instance.Owner.Id;

			if (project.Instance.Product != null)
				projectRow.ProductId = project.Instance.Product.Id;

			projectRow.Status = project.Instance.Status;
			if (project.Instance.TechnicalContact != null)
				projectRow.TechnicalContactId = project.Instance.TechnicalContact.Id;

			if (project.Instance.WorkArea != null)
				projectRow.WorkAreaId = project.Instance.WorkArea.Id;

			if (isNew)
			{
				Storage.Project.AddProjectRow(projectRow);
			}

			foreach (ContentEntity ce in project.Instance.Contents)
			{
				CreateContent(ce, transaction);
			}

			foreach (Translation translation in project.Instance.Translations)
			{
				CreateTranslation(translation, transaction);
			}


			iLangl.LanguageWire.DAL.iLanglDAL.DALFacade.Instance.ProjectDAL.UpdateProjectReference(project.Instance.Id, projectRow.TPIdentity, transaction);
			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		public void CreateContent(Model.ContentEntity content, System.Data.IDbTransaction transaction)
		{
			dtoMockupStorage.ContentRow contentRow = Storage.Content.FindById(content.Id);

			bool isNew = (contentRow == null);

			if (isNew)
			{
				contentRow = Storage.Content.NewContentRow();
			}

			contentRow.Content = content.Content;
			contentRow.Id = content.Id;
			if (content.Language != null)
				contentRow.LanguageCode = content.Language.LanguageId;

			contentRow.Name = content.Name;
			if (content.Project != null)
				contentRow.ProjectId = content.Project.Id;

			contentRow.Type = content.Type;
			contentRow.Version = content.Version;
			
			if (isNew)
			{
				Storage.Content.AddContentRow(contentRow);
			}


			if (transaction == null)
			{
				AcceptChanges();
			}

			iLangl.LanguageWire.DAL.iLanglDAL.DALFacade.Instance.ProjectDAL.UpdateContentReference(content.Id, contentRow.TPIdentity, transaction);
		}




		public void CreateTranslation(Model.Translation translation, System.Data.IDbTransaction transaction)
		{
			dtoMockupStorage.TranslationRow translationRow = Storage.Translation.FindById(translation.Id);

			bool isNew = (translationRow == null);

			if (isNew)
			{
				translationRow = Storage.Translation.NewTranslationRow();
			}

			translationRow.Id = translation.Id;
			translationRow.Progress = translation.Progress;
			translationRow.Comment = translation.Comment;
			translationRow.Status = MappingManager.Instance.GetTranslationStatus(translation.Status);

			if (translation.Project != null)
				translationRow.ProjectId = translation.Project.Id;


			if (translation.LanguagePair != null)
			{
				if (translation.LanguagePair.SourceLanguage != null)
					translationRow.SourceLanguageCode = translation.LanguagePair.SourceLanguage.LanguageId;

				if (translation.LanguagePair.TargetLanguage != null)
					translationRow.TargetLanguageCode = translation.LanguagePair.TargetLanguage.LanguageId;
			}

			if (translation.SourceContent != null)
			{
				translationRow.SourceContentId = translation.SourceContent.Id;
			}

			if (translation.TargetContent != null)
			{
				translationRow.TargetContentId = translation.TargetContent.Id;
			}

			//Assign default contact like responsible
			if (translation.Responsible == null)
			{
				translation.Responsible = DALFacadeLW.Instance.ContactDAL.GetResponsiblePerson(translationRow.TPIdentity);
			}

			translationRow.ResponsibleId = int.Parse(translation.Responsible.TPIdentity);

			if (isNew)
			{
				Storage.Translation.AddTranslationRow(translationRow);
			}


			if (transaction == null)
			{
				AcceptChanges();
			}
			translation.TPIdentity = translationRow.TPIdentity.ToString();
			iLangl.LanguageWire.DAL.iLanglDAL.DALFacade.Instance.TranslationDAL.UpdateTranslation(translation, transaction);
		}

		#endregion


		public JobInfo GetJobInfo(int jobId)
		{
			JobInfo result = null;

			foreach (dtoMockupStorage.TranslationRow tr in Storage.Translation)
			{
				if (tr.TPIdentity == jobId)
				{
					result = new JobInfo();
					result.JobId = tr.TPIdentity;
					result.ParentJobId = tr.TPIdentity;
					result.ResponsiblePerson = tr.ResponsibleId;
					result.Status = tr.Status;
					result.JobType = 1;//Translation

					return result;
				}
			}



			return result;
		}


		public ContentEntity[] DownloadSourceContent(Translation translation)
		{
			return null;
		}

		#region IProjectDAL Members


		public int GetProgress(JobInfo Job)
		{
			return 0;
		}

		#endregion
	}
}